<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <#include "/pubHeadStyle.html">
</head>
<body>
<div style="display: inline-block; padding-left: 25px; width: 95%">
    <div class="layui-btn-container">
        <a class="layui-btn  layui-btn-md tool_btn" id="ok" lay-event="ok">
            <i class="layui-icon layui-icon-ok-circle"></i>
            <span>确定</span>
        </a>
        <a class="layui-btn layui-btn-md tool_btn" lay-event="add" id="add">
            <i class="layui-icon layui-icon-add-1"></i>
            <span>添加</span>
        </a>
        <a class="layui-btn layui-btn-md tool_btn" lay-event="edit" id="edit">
            <i class="layui-icon layui-icon-edit"></i>
            <span>编辑</span>
        </a>
        <a class="layui-btn layui-btn-danger layui-btn-md tool_btn" lay-event="del" id="del">
            <i class="layui-icon layui-icon-delete"></i>
            <span>删除</span>
        </a>
        <a class="layui-btn layui-btn-md tool_btn" lay-event="saveGroupNo" id="saveGroupNo">
            <i class="iconfont iconsave"></i>
            <span class="saveButton" style="cursor: pointer">保存批次号</span>
        </a>
    </div>
    <div>
        <table class="layui-table" id="userConditionTable">
            <thead>
                <tr>
                    <th>
                        <label><input type="checkbox" lay-skin="primary" id="checkAll" lay-filter="checkAll">序号</label>
                    </th>
                    <th>规则描述</th>
                    <th>用户描述</th>
                    <th>位置调整</th>
                    <th>批次号</th>
                </tr>
            </thead>
            <tbody>
                <#list receiverSetting as userCondition>
                    <tr id="${userCondition.id}">
                        <td>
                            <input class="pk" type="checkbox" name="nodeUserCheck" lay-skin="primary" lay-filter="filter" value="${userCondition.id}">${userCondition_index}
                            <input type="hidden" name="sn" value="${userCondition.sn}">
                        </td>
                        <td>
                            <div name="condition" >
                                <textarea style="display:none;">${userCondition.condition}</textarea>
                                <label></label>
                            </div>
                        </td>
                        <td>
                            <label>${userCondition.conditionShow}</label>
                        </td>
                        <td>
                            <a class="iconfont iconlong-arrow-up" onclick="moveTr(this,true)" title="上移"></a>
                            <a class="iconfont iconlong-arrow-down" onclick="moveTr(this,false)"  title="下移"></a>
                        </td>
                        <td name="groupNoTd">
                            <div>
                                <input name="groupNo" class="layui-input" oldValue="${userCondition.groupNo}" value="${userCondition.groupNo}">
                            </div>
                        </td>
                    </tr>
                </#list>
            </tbody>
        </table>
    </div>
    <div style="display: none">
        <textarea id="actDefModel">
            {
                modelId:"${actDefModel.id}",
                procDefId:"${actDefModel.procDefId}",
                nodeId:"${nodeId}"
            }
        </textarea>
        <a href="" id="goLocation" style="display: none"></a>
    </div>
</div>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'jquery', 'form', 'commons','element', 'setter', 'totalFilter', 'common'], function () {
        var table = layui.table,
            layer = layui.layer,
            $ = layui.jquery,
            element = layui.element,
            setter = layui.setter,
            commons = layui.commons,
            form = layui.form;

        var procDefId = '${actDefModel.procDefId}';
        var nodeId = '${nodeId}';
        var modelId = '${actDefModel.id}';
        var conditionType = ${conditionType};
        form.render();
        var selectUserWindowUrl = '/workflow/actDefModel/view/userSetUpdate?modelId='+modelId+'&nodeId='+nodeId + '&conditionType=' + conditionType;
        //人员初始化显示
        $("[name='condition']").each(function () {
            //规则显示，获取condition
            var condition = $("textarea",this).val();
            if(!condition){
                return false;
            }
            //转换为JSON
            var conditionArr = JSON.parse(condition);
            //转换为数组
            var conditionShow ="";
            for(var i=0;i<conditionArr.length;i++) {
                var _json = conditionArr[i];
                if(i!=0) {
                    if (_json.compType == "and") {
                        conditionShow += " 并且 "
                    } else if (_json.compType == "or") {
                        conditionShow += " 或者 "
                    };
                }
                conditionShow += _json.conDesc;
            }
            //添加规则设置的内容
            $("label",this).html(conditionShow);
        });
        $("#checkAll").on("change",function () {
        // form.on('checkbox(checkAll)', function(data){
            var child = $(".pk");
            var that = $(this)
            child.each(function (index, item) {
                $(item).prop("checked", that[0].checked);
            });
            form.render('checkbox');
        })
        $(".pk").on("change",function () {
        // form.on('checkbox(filter)', function(data){
            var item = $(".pk");
            for (var i = 0; i < item.length; i++) {
                if (item[i].checked == false) {
                    $("#checkAll").prop("checked", false);
                    form.render('checkbox');
                    break;
                }
            }
            //如果都勾选了  勾上全选
            var  all=item.length;
            for (var i = 0; i < item.length; i++) {
                if (item[i].checked == true) {
                    all--;
                }
            }
            if(all==0){
                $("#checkAll").prop("checked", true);
                form.render('checkbox');
            }
        })
        $("#add").on('click', function () {
            parent.layer.open({
                title:'配置节点人员',
                type: 2,
                shade: 0.3,
                area: ['1000px', '585px'],
                btn:["保存","关闭"],
                // content: $("#selectUserWindow"),
                content: serverPath.workflowPath +selectUserWindowUrl,
                yes:function (index,layero) {
                    //获取iframe的body元素
                    var body = parent.layer.getChildFrame('body',index);
                    //得到iframe页的窗口对象
                    var iframeWin = parent.window[layero.find('iframe')[0]['name']];
                    //执行iframe页的showMsg方法
                    // iframeWin.showMsg("mercy");
                    iframeWin.save(shuaxin(index));
                    //刷新iframe


                }
            });

        });
function shuaxin(index){
    setTimeout(function () {
        if (localStorage.getItem('pageTabs') === '1'){
            var _iframe1=$(window.parent.document).contents().find("iframe")[$(window.parent.document).contents().find("iframe").length-2];
        }else {
            var _iframe1=$(window.parent.document).contents().find("iframe")[$(window.parent.document).contents().find("iframe").length-2];
        }
        _iframe1.contentWindow.location.reload(true);
        parent.layer.close(index); //如果设定了yes回调，需进行手工关闭
    },1500)

}
        //删除
        $('#del').click(function () {
            var tableObj=$("#userConditionTable");
            var deleteObjs = $('.pk[type=checkbox]:checked',tableObj);
            if(deleteObjs.length==0){
                layer.alert('请选择要删除的记录!');
                return ;
            }else{
                layer.confirm('确定删除吗？',{title:'提示'}, function(index) {
                    var conditionIds = [];
                    $(deleteObjs).each(function (index, item) {
                        var conditionId = $(this).val();
                        if(conditionId!='' && conditionId!=undefined){
                            conditionIds.push(conditionId);
                        }
                    });

                    conditionIds = conditionIds.join(",");

                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: "${ctx}/workflow/bpmUserCondition/del",
                        data:{conditionIds:conditionIds},
                        success: function () {
                            for(var i=deleteObjs.length-1;i>=0;i--){
                                $(deleteObjs[i]).parent().parent().fadeOut(500, function() {
                                    $(this).remove();
                                });
                            }
                        }
                    });

                    layer.close(index);
                });
            }
        });

        //修改
        $('#edit').click(function () {
            var tableObj=$("#userConditionTable");
            var selectChecked = $('.pk[type="checkbox"]:checked',tableObj);
            var conditionId=$(selectChecked).val();
            //是否被选中
            if(selectChecked.length==0){
                layer.alert('请选择一条要修改的记录!');
                return ;
            }

            // 是否选中多条
            if(selectChecked.length>1){
                layer.alert('一次只能对一条记录进行修改!');
                return ;
            }

            //打开弹窗传值过去
            parent.layer.open({
                title:'修改节点人员',
                type:2,
                shade:0.3,
                btn:["保存","关闭"],
                skin: 'btn-class',
                area:  ['1000px', '585px'],
                content:serverPath.workflowPath + setter.workflowLinkPath +"actDefModel/view/userSetUpdate?modelId="+modelId+"&nodeId="+nodeId+"&conditionId="+conditionId,
                end: function(layero, index){
                    element.tabChange('commonSetting', "userSet");
                },
                yes:function (index,layero) {
                    //获取iframe的body元素
                    var body = layer.getChildFrame('body',index);
                    //得到iframe页的窗口对象
                    var iframeWin = parent.window[layero.find('iframe')[0]['name']];
                    //执行iframe页的showMsg方法
                    // iframeWin.showMsg("mercy");


                    iframeWin.save();
                    //刷新iframe
                    var _iframe1=$(window.parent.document).contents().find("iframe")[$(window.parent.document).contents().find("iframe").length-2];
                    _iframe1.contentWindow.location.reload(true);
                    parent.layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        });

        //保存批次号
        $(document).on('click', '.saveButton', function () {
            var conditionIds = [];
            var groupNos = [];
            var tableObj=$("#userConditionTable");

            $('input[name=groupNo]',tableObj).each(function () {
                var _this = $(this);
                if( _this.val()!=_this.attr('ivalue')){
                    var groupNo = _this.val();
                    var tr = _this.closest("tr");
                    var conditionId = $('input[name=nodeUserCheck]',tr).val();
                    conditionIds.push(conditionId);
                    groupNos.push(groupNo);
                }
            });

            if(conditionIds.length == 0){ return; }

            $.ajax({
                url: serverPath.workflowPath + setter.workflowLinkPath + "bpmUserCondition/updateGroup",
                dataType:"json",
                data:{'conditionIds':conditionIds.join(","),'groupNos':groupNos.join(",") },
                success: function (result) {
                    if (commons._filterRecord(result)) {
                        layer.msg('更新批次号成功',{icon: 1});
                    } else {
                        layer.msg('更新批次号失败',{icon: 2});
                    }
                }
            });

        });

        $("#ok").click(function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        })
    //    移动一行
        window.moveTr=function (obj,up){
            var thisTr = $(obj).parents("tr");
            if(up){
                var prevTr = $(thisTr).prev();
                if(prevTr){
                    thisTr.insertBefore(prevTr);
                }
            } else {
                var nextTr = $(thisTr).next();
                if(nextTr){
                    thisTr.insertAfter(nextTr);
                }
            }
        };
    });
</script>
</body>
</html>